# Compiler settings
CC := gcc
CXX := g++
CFLAGS := -Wall
CXXFLAGS := -Wall
LDFLAGS := -Lbuild/lib -lmem_pool

# Directories
SRC_DIR := .
BUILD_DIR := build
LIB_DIR := $(BUILD_DIR)/lib
BIN_DIR := $(BUILD_DIR)/bin

# Source files
C_SOURCES := $(wildcard $(SRC_DIR)/*.c)
CPP_SOURCES := $(wildcard $(SRC_DIR)/*.cpp)
C_OBJECTS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.o,$(C_SOURCES))
CPP_OBJECTS := $(patsubst $(SRC_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(CPP_SOURCES))

# Targets
all: c_example cpp_example

# Compile C sources
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
	mkdir -p $(BUILD_DIR)
	$(CC) $(CFLAGS) -c -o $@ $<

# Compile C++ sources
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp
	mkdir -p $(BUILD_DIR)
	$(CXX) $(CXXFLAGS) -c -o $@ $<

# Build the static library
$(LIB_DIR)/libmem_pool.a: $(C_OBJECTS)
	mkdir -p $(LIB_DIR)
	ar rcs $@ $(C_OBJECTS)

# Build C example
c_example: $(LIB_DIR)/libmem_pool.a $(BUILD_DIR)/c_example.o
	mkdir -p $(BIN_DIR)
	$(CC) $(CFLAGS) -o $(BIN_DIR)/c_example $(BUILD_DIR)/c_example.o $(LDFLAGS)

# Build C++ example
cpp_example: $(LIB_DIR)/libmem_pool.a $(BUILD_DIR)/cpp_example.o
	mkdir -p $(BIN_DIR)
	$(CXX) $(CXXFLAGS) -o $(BIN_DIR)/cpp_example $(BUILD_DIR)/cpp_example.o $(LDFLAGS)

clean:
	rm -rf $(BUILD_DIR)

.PHONY: all clean c_example cpp_example
